﻿
Namespace locks
    Public Structure simplelock
        Implements ilock

        Private se As singleentry

        Shared Sub New()
            yield()
        End Sub

        Public Sub enter() Implements ilock.enter
            wait_until(Function(ByRef s As singleentry) As Boolean
                           Return s.mark_in_use()
                       End Function,
                       se)
        End Sub

        Public Sub [exit]() Implements ilock.exit
            se.release()
        End Sub

        Public Function thread_owned() As Boolean Implements ilock.thread_owned
            Return False
        End Function
    End Structure

    Public Structure simplelock(Of _LOOPS_PER_YIELD As _int64)
        Implements ilock

        Private Shared ReadOnly loops_per_yield As UInt64 = 0
        Private se As singleentry

        Shared Sub New()
            Dim i As Int64 = 0
            i = +alloc(Of _LOOPS_PER_YIELD)()
            assert(i > 0)
            loops_per_yield = i
            yield()
        End Sub

        Public Sub enter() Implements ilock.enter
            wait_until(Function(ByRef s As singleentry) As Boolean
                           Return s.mark_in_use()
                       End Function,
                       loops_per_yield,
                       se)
        End Sub

        Public Sub [exit]() Implements ilock.exit
            se.release()
        End Sub

        Public Function thread_owned() As Boolean Implements ilock.thread_owned
            Return False
        End Function
    End Structure
End Namespace
